package com.xuecheng.content.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.xuecheng.commons.constants.CourseConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.BindTeachplanMediaDto;
import com.xuecheng.commons.model.vo.MediaVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.content.domain.Teachplan;
import com.xuecheng.content.domain.TeachplanMedia;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.TeachplanMediaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.content.service.TeachplanService;
import com.xuecheng.media.api.MediaClient;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author HuangYuwu
 * @since 2022-09-11
 */
@Service
public class TeachplanMediaServiceImpl extends ServiceImpl<TeachplanMediaMapper, TeachplanMedia> implements TeachplanMediaService {

    @Autowired
    private TeachplanService teachplanService;

    @Autowired
    private CourseBaseService courseBaseService;

    @Autowired
    private MediaClient mediaClient;

    /**
     * 课程计划绑定媒资
     *
     * @param bindTeachplanMediaDto
     * @return
     */
    @Override
    public ResponseResult bindTeachplanMedia(BindTeachplanMediaDto bindTeachplanMediaDto) {
        if (ObjectUtil.isEmpty(bindTeachplanMediaDto.getMediaId()) || ObjectUtil.isEmpty(bindTeachplanMediaDto.getTeachplanId())) {
            throw new BusinessException(ErrorCode.PARAMSERROR);
        }
        Teachplan teachplan = teachplanService.getById(bindTeachplanMediaDto.getTeachplanId());
        ResponseResult<MediaVo> result = mediaClient.findMeidaById(bindTeachplanMediaDto.getMediaId());
        CourseBase courseBase = courseBaseService.getById(teachplan.getCourseId());
        String auditStatus = courseBase.getAuditStatus();
        if (StrUtil.equals(auditStatus, CourseConstants.AUDIT_COMMIT_STATUS) ||
                StrUtil.equals(auditStatus, CourseConstants.AUDIT_PASTED_STATUS) ||
                StrUtil.equals(auditStatus, CourseConstants.AUDIT_PUBLISHED_STATUS)
        ) {
            throw new BusinessException(ErrorCode.UPDATEEERROR);
        }
        Integer code = result.getCode();
        if (code != 0) {
            throw new BusinessException(ErrorCode.UPDATEEERROR);
        }
        MediaVo mediaVo = result.getData();
        //若MediaClient返回值没有指定泛型 需要先转JSON才能转成对象
        //MediaVo mediaVo = JSON.parseObject(JSON.toJSONString(result.getData()),MediaVo.class);
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        teachplanMedia.setMediaId(mediaVo.getId());
        teachplanMedia.setTeachplanId(teachplan.getId());
        teachplanMedia.setMediaFilename(mediaVo.getFilename());
        teachplanMedia.setCourseId(teachplan.getCourseId());
        teachplanMedia.setCoursePubId(teachplan.getCoursePubId());
        teachplanMedia.setCreateDate(DateUtil.date());
        teachplanMedia.setCreatePeople(AuthInfoHolder.getAuthInfo().getUserName());
        teachplanMedia.setCompanyId(AuthInfoHolder.getAuthInfo().getCompanyId());
        this.save(teachplanMedia);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteTeachMedia(Long teachPlanMediaId) {
        if (ObjectUtil.isEmpty(teachPlanMediaId)) {
            throw new BusinessException(ErrorCode.PARAMSERROR);
        }
        TeachplanMedia teachplanMedia = this.getById(teachPlanMediaId);
        Teachplan teachplan = teachplanService.getById(teachplanMedia.getTeachplanId());
        CourseBase courseBase = courseBaseService.getById(teachplan.getCourseId());
        String auditStatus = courseBase.getAuditStatus();
        if (StrUtil.equals(auditStatus, CourseConstants.AUDIT_COMMIT_STATUS) ||
                StrUtil.equals(auditStatus, CourseConstants.AUDIT_PASTED_STATUS) ||
                StrUtil.equals(auditStatus, CourseConstants.AUDIT_PUBLISHED_STATUS)
        ) {
            throw new BusinessException(ErrorCode.UPDATEEERROR);
        }
        this.removeById(teachPlanMediaId);
        return ResponseResult.okResult();
    }
}
